package cn.jtcoding.user.web.controller;


import cn.jtcoding.user.api.UserAddressService;
import cn.jtcoding.user.pojo.bo.UserAddressBO;
import cn.jtcoding.user.pojo.dataobject.UserAddressDO;
import cn.jtcoding.common.framework.enums.ApiResultCode;
import cn.jtcoding.common.framework.vo.ApiResult;
import cn.jtcoding.web.framework.annotation.Idempotent;
import cn.jtcoding.web.framework.annotation.IdempotentParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class UserAddressController {

    /**
     * 用户在确认订单页面，可以针对收货地址做如下操作：
     * 1. 查询用户的所有收货地址列表
     * 2. 新增收货地址
     * 3. 删除收货地址
     * 4. 修改收货地址
     * 5. 设置默认地址
     */

    @Autowired
    private UserAddressService addressService;

    @GetMapping("/list")
    public ApiResult<?> list(@RequestParam String userId) throws InterruptedException {
        if (StringUtils.isBlank(userId)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }
        List<UserAddressDO> list = addressService.listUserAddress(userId);
        return ApiResult.success(list);
    }

    @PostMapping("/add")
    @Idempotent(period = 5)
    public ApiResult<?> add(@IdempotentParam @RequestBody UserAddressBO userAddressBO) {

        ApiResult<?> checkRes = checkAddress(userAddressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }
        addressService.addUserAddress(userAddressBO);

        return ApiResult.success();
    }
    private ApiResult<?> checkAddress(UserAddressBO userAddressBO) {
        String receiver = userAddressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }
        if (receiver.length() > 12) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        String mobile = userAddressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }
        if (mobile.length() != 11) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        String province = userAddressBO.getProvince();
        String city = userAddressBO.getCity();
        String district = userAddressBO.getDistrict();
        String detail = userAddressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        return ApiResult.success();
    }

    @PostMapping("/update")
    public ApiResult<?> update(@RequestBody UserAddressBO userAddressBO) {

        if (StringUtils.isBlank(userAddressBO.getAddressId())) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        ApiResult<?> checkRes = checkAddress(userAddressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }

        addressService.updateUserAddress(userAddressBO);

        return ApiResult.success();
    }

    @PostMapping("/delete")
    public ApiResult<?> delete(
            @RequestParam String userId,
            @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        addressService.deleteUserAddress(userId, addressId);
        return ApiResult.success();
    }

    @PostMapping("/setDefalut")
    public ApiResult<?> setDefalut(
            @RequestParam String userId,
            @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        addressService.updateUserAddressToBeDefault(userId, addressId);
        return ApiResult.success();
    }

    @GetMapping("/{userId}/{addressId}")
    public ApiResult<UserAddressDO> queryUserAddress(@PathVariable String userId, @PathVariable String addressId) {
        return ApiResult.success(addressService.queryUserAddress(userId, addressId));
    }
}
